<template>
    <!-- 侧边菜单栏 -->
    <el-menu default-active="home" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
        :collapse="isCollapse" background-color="#333333" text-color="#fff" active-text-color="#ffd04b">
        <!-- 标题 -->
        <h3>{{ isCollapse ? '后台' : '后台管理系统' }}</h3>
        <!-- 一级菜单 -->
        <el-menu-item @click="clickMenu(item)" v-for="item in noChilden" :key="item.name" :index="item.name">
            <i :class="`el-icon-${item.icon}`"></i>
            <span slot="title">{{ item.label }}</span>
        </el-menu-item>
        <!-- 其他导航 二级菜单嵌套 -->
        <el-submenu v-for="item in hasChilden" :key="item.label" :index="item.label">
            <!-- 一级菜单 -->
            <template slot="title">
                <i :class="`el-icon-${item.icon}`"></i>
                <span slot="title">{{ item.label }}</span>
            </template>
            <!-- 子菜单 -->
            <el-menu-item-group v-for="subitem in item.children" :key="subitem.path" :index="subitem.path">
                <!-- 也可以在el-menu中使用<router-link to="/">Home</router-link>-->
                <el-menu-item @click="clickMenu(subitem)" :index="subitem.path">{{ subitem.label }}</el-menu-item>
            </el-menu-item-group>
        </el-submenu>

    </el-menu>
</template>


<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;

}

.icons {
    width: 18px;
    height: 18px;
    margin-right: 5px;
}

.el-menu {
    height: 100vh;
    // border-right: solid 1px #e6e6e6;
    border-right: none;
    background-color: #545c64;

    .el-menu-item,
    .el-submenu {
        max-width: 100%;
        /* 确保菜单项宽度不超出父容器 */
    }

    /* 去掉滚动条 */
    h3 {
        line-height: 48px;
        color: #fff;
        text-align: center;
    }
}

.el-aside {
    height: 100%;
    background-color: #545c64;
}
</style>

<script>
export default {
    data() {
        return {
            menuData: [
                {
                    path: '/home',
                    name: 'home',
                    label: '首页',
                    icon: 'house',
                    url: 'Home'
                },
                {
                    path: '/mall',
                    name: 'mall',
                    label: '商品管理',
                    icon: 'video-play',
                    url: 'Mall'
                },
                {
                    path: '/user',
                    name: 'user',
                    label: '用户管理',
                    icon: 'user',
                    url: 'User'
                },
                {
                    path: 'other',
                    label: '其他',
                    icon: 'location',
                    children: [
                        {
                            path: '/page1',
                            name: 'page1',
                            label: '页面1',
                            icon: 'setting',
                            url: 'Page1'
                        },
                        {
                            path: '/page2',
                            name: 'page2',
                            label: '页面2',
                            icon: 'setting',
                            url: 'Page2'
                        }
                    ]
                }
            ]
        };
    },
    methods: {
        handleOpen(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        },
        clickMenu(item) {
            console.log(item);
            if (this.$route.path !== item.path && !(this.$route.path === '/home' && (item.path === '/'))) {
                this.$router.push(item.path)
            }
            //使用commit调用store中的同步内容
            this.$store.commit('selectMenu', item)
        }
    },
    computed: {
        //没有子菜单
        noChilden() {
            return this.menuData.filter(item => !item.children)
        },
        hasChilden() {
            return this.menuData.filter(item => item.children)
        },
        // 控制菜单栏的收起与折叠
        isCollapse() {
            return this.$store.state.tab.isCollapse
        }

    }
}
</script>
